package com.dayuanit.dy15.atm.listener;

import com.dayuanit.dy15.atm.bean.BeanFactory;
import com.dayuanit.dy15.atm.bean.DefaultBeanFactory;
import com.dayuanit.dy15.atm.util.BeanFactoryUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
import java.util.Properties;

//tomcat jetty 这些WEB应用服务器也叫 servlet容器
public class AtmServletContextLister implements ServletContextListener {

    /**
     * 服务器启动的时候 执行的初始化方法
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println(">>>>>>> AtmServletContextLister init()<<<<<<<<<<<");
        try {
            //当前的服务器的实例对象 可以理解为 就是tomcat jetty
            ServletContext servletContext = sce.getServletContext();
            String packagePath = servletContext.getInitParameter("packagePath");
            System.out.println("packagePath=" + packagePath);

            InputStream resourceAsStream = AtmServletContextLister.class.getClassLoader().getResourceAsStream(packagePath);
            Properties properties = new Properties();
            properties.load(resourceAsStream);

            String packageNames = properties.getProperty("package.name");

            BeanFactory beanFactory = BeanFactoryUtil.beanFactory;
            beanFactory.builder(packageNames.split(","));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
